package com.houtai.controller.article;


import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.ResultVo;
import com.houtai.bean.dto.article.ArticleDetailDto;
import com.houtai.bean.dto.article.ArticleListDto;
import com.houtai.bean.dto.article.comments.TsArticleCommentsInsertDto;
import com.houtai.bean.dto.article.comments.TsArticleCommentsListDto;
import com.houtai.bean.vo.article.ArticleDetailVo;
import com.houtai.bean.vo.article.ArticleListVo;
import com.houtai.bean.vo.article.cate.TsArticleCateListVo;
import com.houtai.bean.vo.article.comments.TsArticleCommentsListVo;
import com.houtai.service.article.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value="文章接口", tags={"文章接口"})
@RequestMapping(value = {"api/v1"}, method = RequestMethod.POST)
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    @ApiOperation(value="文章类别列表", notes="文章类别列表")
    @RequestMapping(value = "/article/cate/list")
    @Authority(AuthorityType.NoValidate)
    public TsArticleCateListVo cate() {
        return articleService.articleCateList();
    }


    @ApiOperation(value="文章列表", notes="文章列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/article/list")
    public ArticleListVo list(@ApiParam(value = "文章列表所需参数") @Valid @RequestBody ArticleListDto dto, BindingResult result) {
        ArticleListVo retInfo = new ArticleListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = articleService.articleList(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="文章详情", notes="文章详情")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/article/detail")
    public ArticleDetailVo detail(@ApiParam(value = "文章详情所需参数") @Valid @RequestBody ArticleDetailDto dto, BindingResult result) {
        ArticleDetailVo retInfo = new ArticleDetailVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = articleService.articleDetail(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="文章评论", notes="文章评论")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/article/comments")
    public ResultVo comments(@ApiParam(value = "文章评论所需参数") @Valid @RequestBody TsArticleCommentsInsertDto dto, BindingResult result,
                             @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = articleService.articleComments(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="文章评论列表", notes="文章评论列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/article/comments/list")
    public TsArticleCommentsListVo comments(@ApiParam(value = "文章评论列表所需参数") @Valid @RequestBody TsArticleCommentsListDto dto, BindingResult result) {
        TsArticleCommentsListVo retInfo = new TsArticleCommentsListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = articleService.commentsList(dto);
        }
        return retInfo;
    }
}
